package midend.IR_Optimizer;

import midend.IR_Module;
import midend.IR_Value.IR_FuncBlock;
import config.Config;
import java.util.ArrayList;
import java.util.List;

public class Optimizer_Factory {
    private final List<IR_Opt> ir_Ops;
    private IR_Module ir_Module;

    public Optimizer_Factory(IR_Module ir_Module) {
        this.ir_Module = ir_Module;
        this.ir_Ops = new ArrayList<>();
//         添加所有优化器
        //ir_Ops.add(new Mem_To_Reg());
        if (Config.is_O1) {
            ir_Ops.add(new Const_Propagation());
            ir_Ops.add(new BrCmp_Opt());
            //ir_Ops.add(new Tail_Recursion_Eli());
            ir_Ops.add(new Common_Subexpression_Eli());
            ir_Ops.add(new Aluopt());
            ir_Ops.add(new GCSE_Optimizer());
            ir_Ops.add(new Branch_Eli(ir_Module));
        }
    }

    public Optimizer_Factory(IR_Module ir_Module,
                             List<IR_Opt> ir_Ops) {
        this.ir_Module = ir_Module;
        this.ir_Ops = new ArrayList<>();
//         添加所有优化器
        for(IR_Opt ir_Opt : ir_Ops) {
            this.ir_Ops.add(ir_Opt);
        }
    }

    public void optimize(IR_FuncBlock ir_funcBlock) {
        for (IR_Opt opt : ir_Ops) {
            opt.Optimize(ir_funcBlock);
        }
    }
}
